package cn.ezios.huo.system.biz.menu;


import cn.ezios.huo.system.biz.login.LoginEvent;
import cn.ezios.huo.system.dao.domain.SysMenu;
import cn.ezios.huo.system.dao.service.SysMenuService;
import cn.ezios.huo.system.dao.service.SysRoleMenuService;
import cn.ezios.huo.system.dao.service.SysUserRoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class MenuService {

    private final SysMenuService sysMenuService;

    private final SysRoleMenuService sysRoleMenuService;
    private final SysUserRoleService sysUserRoleService;

    public static final String MENU_TYPE_CATALOG = "CATALOG";
    public static final String MENU_TYPE_MENU = "MENU";


    public static final String MENU_TREE_ROOT = "0";

    /**
     * 创建菜单目录
     *
     * @param menuCatalogName
     * @param parentId
     * @return
     */
    public SysMenu creatMenuCatalog(String menuCatalogName, String parentId){
        SysMenu sysMenu = new SysMenu();
        sysMenu.setMenuName(menuCatalogName);
        sysMenu.setParentId(parentId);
        sysMenu.setMenuType(MENU_TYPE_CATALOG);
        sysMenuService.save(sysMenu);
        return sysMenu;
    }

    /**
     * 创建菜单
     * @param menuName
     * @param parentId
     * @return
     */
    public SysMenu creatMenu(String menuName, String parentId){
        SysMenu sysMenu = new SysMenu();
        sysMenu.setMenuName(menuName);
        sysMenu.setParentId(parentId);
        sysMenu.setMenuType(MENU_TYPE_MENU);
        sysMenuService.save(sysMenu);
        return sysMenu;
    }

    /**
     * 组合菜单树林
     * @param menuIds
     * @return
     */
    public List<MenuVo> getMenuTreeByMenuIds(String[] menuIds){
        if(menuIds==null||menuIds.length<1){
            return List.of();
        }
        List<SysMenu> list = sysMenuService.lambdaQuery()
                .in(SysMenu::getMenuId, menuIds)
                .list();
        List<MenuVo> MenuTreeList = buildMenuTree(list);
        return MenuTreeList;
    }
    public List<MenuVo> getMenuTreeByMenuIds(List<String> menuIds){
        if(menuIds.isEmpty()){
            return List.of();
        }
        List<SysMenu> list = getSysMenus(menuIds);
        List<MenuVo> MenuTreeList = buildMenuTree(list);
        return MenuTreeList;
    }

    public List<SysMenu> getSysMenus(List<String> menuIds) {
        List<SysMenu> list = sysMenuService.lambdaQuery()
                .in(SysMenu::getMenuId, menuIds)
                .list();
        return list;
    }

    public List<MenuVo> buildMenuTree(List<SysMenu> list) {
        List<MenuVo> MenuTreeList = list.stream()
                .filter(menu -> menu.getParentId().equals(MENU_TREE_ROOT))
                .map(menu -> recursiveMenuTree(menu, list))
                .toList();
        return MenuTreeList;
    }

    public List<MenuVo> getCatalogTree(){
        List<SysMenu> list = sysMenuService.lambdaQuery().eq(SysMenu::getMenuType, MENU_TYPE_CATALOG)
                .list();
        MenuVo menuVo = new MenuVo();
        menuVo.setMenuId(MenuService.MENU_TREE_ROOT);
        menuVo.setMenuName("根目录");
        menuVo.setChildren(buildMenuTree(list));
        LinkedList<MenuVo> menuVos = new LinkedList<>();
        menuVos.add(menuVo);
        return menuVos;
    }

    /**
     * 根据角色id获取菜单树
     * @param roleIds
     * @return
     */
    public List<MenuVo> getMenuTreeByRoleIds(List<String> roleIds){
        List<String> menuIds = sysRoleMenuService.getMenuIdsByRoleIds(roleIds);
        if(!menuIds.isEmpty())
            return getMenuTreeByMenuIds(menuIds);
        return List.of();
    }

    public static void main(String[] args) {
        List<String> list = List.of("1", "2", "3");
        String[] listArray = list.toArray(new String[0]);
        System.out.println(listArray);
    }

    public List<String> getMenuIdsByUserId(String userId) {
        List<String> roleIds =
                sysUserRoleService.getUserRoleIds(userId);
        List<String> menuIds =
                sysRoleMenuService.getMenuIdsByRoleIds(roleIds);
        return menuIds;
    }

    /**
     * 递归生成单棵菜单树
     * @param sysMenu
     * @param list
     * @return
     */
    private MenuVo recursiveMenuTree(SysMenu sysMenu, List<SysMenu> list){
        MenuVo vo = new MenuVo();
        BeanUtils.copyProperties(sysMenu, vo);
        List<MenuVo> children = list.stream()
                // 过滤出当前菜单的子菜单
                .filter(menu -> menu.getParentId().equals(sysMenu.getMenuId()))
                // 子菜单递归调用
                .map(menu -> recursiveMenuTree(menu, list))
                .toList();
        if(!children.isEmpty())
            vo.setChildren(children);
        return vo;
    }


}
